package edu.wit.common.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yyh
 * @create 2020-10-30 15:46
 */
public class DateUtil {
	public static final String DEFAULT_NOT_SPLIT_SAMPLE_FORMAT = "yyyyMMddHHmmss";
	
	public static final String DEFAULT_DROP_SAMPLE_FORMAT = "yyyy.MM.dd";

    public static final String DEFAULT_DATE_SAMPLE_FORMAT = "yyyy-MM-dd";

    public static final String DEFAULT_TIME_SAMPLE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DEFAULT_TIME_NOS_SAMPLE_FORMAT = "yyyy-MM-dd HH:mm";

    public static final String DEFAULT_DATE_CHINESE_FORMAT = "yyyy年MM月dd日";

    public static final String DEFAULT_YEAR_CHINESE_FORMAT = "yyyy年";

    public static final String DEFAULT_MONTH_CHINESE_FORMAT = "yyyy年MM月";

    public static final String TIME_ZONE = "GMT+8";
    
    public static final String DEFAULT_TIME_MILLISECOND_FORMAT = "HH:mm:ss:SSS";
    
	private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(DEFAULT_DATE_SAMPLE_FORMAT);
    
    public static Date getDropDate(String dateStr){
		SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_DROP_SAMPLE_FORMAT);
		Date date=null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
		
	}
    
    
	/**
	 * 将秒字符串转成时分秒
	 */
	public static String getTimeByStr(String timeStr){
		if(StringUtils.isNotBlank(timeStr)){
			Long time = Long.parseLong(timeStr);
			//时
			Long h = time/3600;
			//不足小时的部分
			Long ms = time%3600;
			//分
			Long m = ms/60;
			//不足分的部分(秒)
			Long s = ms%60;
			timeStr = "";
			if(h != 0) {
				timeStr = h + "时" + m + "分" + s + "秒";
			} else if(m != 0) {
				timeStr = m + "分" + s + "秒";;
			} else {
				timeStr = s + "秒";
			}
		}
		return timeStr;
	}
	
	/**
	 * 获取提前多少天的日期
	 * @param dateNum 天数
	 * @return
	 */
	public static Date getBeforeDate(int dateNum){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -dateNum);
		return cal.getTime();
	}

    /**
     * 字符串转时间类型
     * @author zzy
     * 2020-04-14 17:59:23
     * @param dateStr		要转化的字符串
     * @param format		时间格式如：yyyy-MM-dd
     * @return
     */
    public static Date getDate(String dateStr, String format){
        Date date=null;
        try {
            if(StringUtils.isNotBlank(dateStr) && StringUtils.isNotBlank(format)){
                SimpleDateFormat sdf=new SimpleDateFormat(format);
                date = sdf.parse(dateStr);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
    
	/**
	 * 转换日期到字符串  不带十 分 秒 
	 * @param date
	 * @return		yyyy-MM-dd 
	 */
	public static synchronized String getDateSampleString(Date date) {
		if (date == null){
			return null;
		}
			
		return DATE_FORMATTER.format(date);
	}

    /**
     * 转换日期到字符串
     * @param date
     * @return		yyyy-MM-dd HH:mm:ss
     */
    public static synchronized String getDateTimeString(Date date) {
        if (date == null){
            return null;
        }
        SimpleDateFormat dateFormater = new SimpleDateFormat(DEFAULT_TIME_SAMPLE_FORMAT);
        return dateFormater.format(date);
    }
    
    /**
	 * 按照指定格式化日期  返回日期字符串
	 * @param date		时间
	 * @param format	转化的时间格式如： yyyy-MM-dd
	 * @return
	 */
	public static String getDateSampleString(Date date, String format) {
		if (date == null){
			return null;
		}
			
		if (format == null){
			return getDateSampleString(date);
		}
			
		SimpleDateFormat dateFormater = new SimpleDateFormat(format);
		return dateFormater.format(date);
	}

	public static Date stringToDate(String dateStr) {
		//MM大写月份，mm分钟
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddl");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
}
